<template>
  <el-dropdown @command="handleCommand">
  <span class="el-dropdown-link">
  <div style="display: flex;align-items: center;color: #ffffff;cursor: pointer">
    <div class="user-icon">
      <i class="el-icon-user" style="font-size: 14px"/>
    </div>
    <span :style="{fontSize:'16px',color}">{{userInfo.userName}}<i class="el-icon-caret-bottom"/></span>
  </div>
  </span>
    <el-dialog :title="dialog.title" :visible.sync="dialog.dialogFormVisible" @close="closeDialog"
               :modal-append-to-body="false" width="30%">
      <el-form :model="modifyPasswordModel" ref="modifyPasswordForm" label-width="100px"
               :rules="modifyPasswordRules"
               size="small ">
        <el-form-item label="用户名">
          <el-input v-model="userInfo.userName" placeholder="用户名" disabled/>
        </el-form-item>
        <el-form-item label="旧密码" prop="oldPassWord">
          <el-input v-model="modifyPasswordModel.oldPassWord" placeholder="请输入旧密码" show-password/>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassWord">
          <el-input v-model="modifyPasswordModel.newPassWord" placeholder="请输入新密码" show-password/>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="modifyPasswordModel.confirmPassword" placeholder="请确认密码" show-password/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.dialogFormVisible = false" size="medium"> 取 消</el-button>
        <el-button type="primary" @click="submitModifyPassword" size="medium"> 保 存</el-button>
      </div>
    </el-dialog>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item command="profile" icon="fa fa-user">个人中心</el-dropdown-item>
      <el-dropdown-item command="modifyPassword" icon="fa fa-key">修改密码</el-dropdown-item>
      <el-dropdown-item command="logout" icon="fa fa-sign-out" divided>退出登录</el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script lang="ts">
import {Component, Vue, Prop, Ref} from 'vue-property-decorator';
import {Action, Getter} from "vuex-class";
import {MyDialog} from "*.vue";
@Component
export default class UserSelect extends Vue {
  @Prop()
  private color!: string;
  @Action('LogOut') private LogOut!: () => Promise<any>;
  @Getter('userInfo') private userInfo!: any;
  private dialog: MyDialog = {
    dialogFormVisible: false,
    title: '修改密码',
  };
  @Ref('modifyPasswordForm') private mdPasswordForm: any;
  private disk = 'false';
  modifyPasswordModel: any = {
    oldPassWord: '',
    newPassWord: '',
    confirmPassword: '',
  };
  private validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
      callback(new Error('请再次输入密码'));
    } else if (value !== this.modifyPasswordModel.newPassWord) {
      callback(new Error('两次输入密码不一致!'));
    } else {
      callback();
    }
  };

  private modifyPasswordRules: any = {
    oldPassWord: [
      {required: true, message: '原密码不能为空', trigger: ['blur']},
    ],
    newPassWord: [
      {required: true, message: '新密码不能为空', trigger: ['blur']},
    ],
    confirmPassword: [
      {required: true, message: '确认密码不能为空', trigger: ['blur']},
      {validator: this.validatePass, trigger: 'blur'},
    ],
  };
  /*重置表单*/
  reset(): void {
    this.mdPasswordForm.resetFields();
  }

  /*关闭对话框*/
  closeDialog() {
    this.$nextTick(() => {
      this.reset();
    });
  }
  public handleCommand(command: string) {
    if (command === 'logout') {
      this.$confirm('确定退出登录?', '退出登录', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        return this.LogOut();
      }).then(() => {
        this.$router.replace('/login')
        // location.reload();
      }).catch((e) => {
        console.error(e);
      });
    } else if (command === 'profile') {
      this.$router.push('/user/profile').catch((e) => {
        console.error(e);
      });
    } else if (command === 'disk') {
      window.open(process.env.VUE_APP_DISK + "/?token=" + this.$store.getters.token, "_blank")
    } else if (command === 'modifyPassword') {
      this.dialog.dialogFormVisible = true;
    }
  }
  submitModifyPassword(){
    console.log("方法空了 ")
  }
}
</script>
<style lang="scss" scoped>
.user-icon{
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #448b89;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
}
</style>
